<?php

include_once(APP_URL.'protected/myLib/qiNiuLib/Pili.php');
define('ACCESS_KEY', '8J1t7om30mbhlXPwcE5l048Qh3ogpA5K2-Xx0kAB');
define('SECRET_KEY', 'WgfOtdFci8oK6NMzFRfoun8r7uT_oJ7aFIw3C3Rl');
define('HUB', 'idooboo');


/*
 * one class wraaper of QiNiu-Sdk
 */
class VideoUtil
{
    var $TAG = 'Class VideoUtil';
    var $stream = NULL;
    
    /**
    * Create a new Stream
    *
    * @param string $aimUrl
    * @return viod
    */
    public function createStream($title, $publishKey, $publishSecurity) {
        // Instantiate an Hub object
        $credentials = new \Qiniu\Credentials(ACCESS_KEY, SECRET_KEY); #=> Credentials Object
        $hub = new \Pili\Hub($credentials, HUB); # => Hub Object
        try {
//             $title           = NULL;     // optional, auto-generated as default
//             $publishKey      = NULL;     // optional, auto-generated as default
//             $publishSecurity = NULL;     // optional, can be "dynamic" or "static", "dynamic" as default
        
            $this->stream = $hub->createStream($title, $publishKey, $publishSecurity); # => Stream Object
            /*
             echo $this->stream->id;
             echo $this->stream->createdAt;
             echo $this->stream->updatedAt;
             echo $this->stream->title;
             echo $this->stream->hub;
             echo $this->stream->disabled;
             echo $this->stream->publishKey;
             echo $this->stream->publishSecurity;
             echo $this->stream->hosts;
             echo $this->stream->hosts["publish"]["rtmp"];
             echo $this->stream->hosts["live"]["rtmp"];
             echo $this->stream->hosts["live"]["http"];
             echo $this->stream->hosts["playback"]["http"];
             */
        
        } catch (Exception $e) {
            echo 'createStream() failed. Caught exception: ',  $e->getMessage(), "\n";
        }
    }
    
    /*
     * Get Stream
     */
    public function getStream($streamId) {
        // Instantiate an Hub object
        $credentials = new \Qiniu\Credentials(ACCESS_KEY, SECRET_KEY); #=> Credentials Object
        $hub = new \Pili\Hub($credentials, HUB); # => Hub Object
        try {
            return $hub->getStream($streamId); # => Stream Object
        } catch (Exception $e) {
            echo "getStream() failed. Caught exception: ",  $e->getMessage(), "\n";
        }
    }

    /*
     * List streams
     */
    public function listStreams() {
        // Instantiate an Hub object
        $credentials = new \Qiniu\Credentials(ACCESS_KEY, SECRET_KEY); #=> Credentials Object
        $hub = new \Pili\Hub($credentials, HUB); # => Hub Object
        try {
            $marker       = NULL;      // optional
            $limit        = NULL;      // optional
            $title_prefix = NULL;      // optional
            $result = $hub->listStreams($marker, $limit, $title_prefix); # => Array
            echo "listStreams() =>\n";
            var_export($result);
            echo "\n\n";
        
        } catch (Exception $e) {
            echo "listStreams() failed. Caught exception: ",  $e->getMessage(), "\n";
        }
    }
    
    /*
     * To JSON string
     */
    public function streamToJsonString() {
        return $this->stream->toJSONString(); # => string
    }
    
    /*
     * Update a Stream
     */
    public function streamUpdate() {
        try {
        
            $this->stream->publishKey      = 'new_secret_words'; // optional
            $this->stream->publishSecurity = 'static';           // optional, can be "dynamic" or "static"
            $this->stream->disabled        = NULL;               // optional, can be "true" of "false"
        
            $this->stream = $this->stream->update(); # => Stream Object
        
            echo "Stream update() =>\n";
            var_export($this->stream);
            echo "\n\n";
        
        } catch (Exception $e) {
            echo "Stream update() failed. Caught exception: ",  $e->getMessage(), "\n";
        }
    }

    /*
     * Disable a Stream
     */
    public function streamDisable() {
        $this->stream = $this->stream->disable(); # => Stream Object
    }

    /*
     * Enable a Stream
     */
    public function streamEnable() {
        $this->stream = $this->stream->enable(); # => Stream Object
    }

    /*
     * Get Stream status
     */
    public function streamGetStatus() {
        try {
            $result = $this->stream->status(); # => Array
            return $result;
        } catch (Exception $e) {
            echo "Stream status() failed. Caught exception: ",  $e->getMessage(), "\n";
        }
    }

    /*
     * Generate RTMP publish URL
     */
    public function streamGenerateRtmpPublishUrl() {
        return $this->stream->rtmpPublishUrl();
    }

    /*
     * Generate RTMP live play URLs
     */
    public function streamGenerateRtmpPlayUrl() {
        $urls = $this->stream->rtmpLiveUrls(); 
        return $urls['ORIGIN']; 
    }

    /*
     * Generate HLS play URLs
     */
    public function streamGenerateHlsPlayUrl() {
        $urls = $this->stream->hlsLiveUrls(); 
        return $urls['ORIGIN']; 
    }

    /*
     * Generate Http-Flv live play URLs
     */
    public function streamGenerateHttpflvPlayUrl() {
        $urls = $this->stream->httpFlvLiveUrls(); 
        return $urls['ORIGIN']; 
    }

    /*
     * Get Stream segments
     */
    public function streamGetStreamSegments() {
        try {
        
            $start = NULL;    // optional, in second, unix timestamp
            $end   = NULL;    // optional, in second, unix timestamp
            $limit = NULL;    // optional, uint
        
            $result = $this->stream->segments($start, $end, $limit); # => Array
        
            echo "Stream segments() =>\n";
            var_export($result);
            echo "\n\n";
        
        } catch (Exception $e) {
            echo "Stream segments() failed. Caught exception: ",  $e->getMessage(), "\n";
        }
    }

    /**
     * Generate HLS playback URLs
     * @param unknown $start    in second, unix timestamp
     * @param unknown $end      in second, unix timestamp
     * @return Ambigous <string>
     */
    public function streamGenerateHlsPlaybackUrl($start, $end) {
        $urls = $this->stream->hlsPlaybackUrls($start, $end);
        return $urls['ORIGIN']; 
    }

    /*
     * Snapshot Stream
     */
    public function streamSnapshot() {
        try {
        
            $name      = 'imageName.jpg'; // required
            $format    = 'jpg';       // required
            $time      = 1440196100;  // optional, in second, unix timestamp
            $notifyUrl = NULL;        // optional
        
            $result = $this->stream->snapshot($name, $format, $time, $notifyUrl); # => Array
        
            echo "Stream snapshot() =>\n";
            var_export($result);
            echo "\n\n";
        
        } catch (Exception $e) {
            echo "Stream snapshot() failed. Caught exception: ",  $e->getMessage(), "\n";
        }
    }

    /*
     * Save Stream as a file
     */
    public function streamSaveToFile($name, $format, $start, $end, $notifyUrl) {
        try {
            return $this->stream->saveAs($name, $format, $start, $end, $notifyUrl = NULL);
        } catch (Exception $e) {
            echo "Stream saveAs() failed. Caught exception: ",  $e->getMessage(), "\n";
        }
    }

    /*
     * Delete a Stream
     */
    public function streamDelete() {
        try {
            $result = $this->stream->delete(); # => NULL
            echo "Stream delete() =>\n";
            var_dump($result);
            echo "\n\n";
        } catch (Exception $e) {
            echo "Stream delete() failed. Caught exception: ",  $e->getMessage(), "\n";
        }
    }
    
    
}
	